
<template>
  <div class="app-container">

    <div class="filter-container">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-input
            v-model="form.keyword"
            placeholder="输入关键词搜索"
            class="input-with-select"
            @keypress.enter.native="getList"
          />
        </el-col>
        <el-col :span="4">
          <el-select
            v-model="form.status"
            style="width:100%"
            placeholder="请选择状态"
            @change="getList"
          >
            <el-option
              v-for="(label, key) in userStatus"
              :key="key"
              :label="label"
              :value="key"
            />
          </el-select>
        </el-col>
        <el-col :span="12">
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="getList"
          >搜索</el-button>
          <el-button
            class="filter-item"
            style="margin-left: 10px;"
            icon="el-icon-edit"
            @click="addMember"
          >
            添加
          </el-button>
        </el-col>

      </el-row>
    </div>

    <el-table
      ref="multipleTable"
      :v-loading="listLoading"
      :data="list"
      height="calc(100vh - 230px)"
      style="width: 100%;margin-bottom: 20px;"
      tooltip-effect="dark"
      :header-cell-style="{ background: '#eaf1fd', color: '#333' }"
      row-key="menu_id"
      border
    >

      <el-table-column
        label="用户名"
        prop="user_name"
        width="180px"
      />

      <el-table-column
        label="用户昵称"
        prop="nick_name"
        width="230px"
      />

      <el-table-column
        label="联系方式"
        prop="tel"
        width="180px"
      />

      <el-table-column
        label="邮箱"
        prop="email"
        width="180px"
      />

      <el-table-column
        label="简介"
        prop="desc"
        min-width="200px"
      />

      <el-table-column
        label="状态"
        width="160px"
      >
        <template slot-scope="{ row }">
          <el-select
            v-model="row.status"
            placeholder="请选择状态"
            @change="memberStatusChanged(row, $event)"
          >
            <el-option
              v-for="(label, key) in userStatus"
              :key="key"
              :label="label"
              :value="+key"
            />
          </el-select>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="right"
        width="150"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            @click="editMember(row)"
          >
            Edit
          </el-button>

          <el-dropdown style="margin-left: 10px;">
            <el-button type="primary">
              更多<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <router-link :to="`/member/app?uid=${row.uid}`">查看应用</router-link>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <UpdateMember
      :visible.sync="updateMember.visible"
      :status="updateMember.status"
      :data.sync="updateMember.data"
    />

  </div>
</template>

<script>
import { memberList, memberStatus } from '@/api/member/index'

import UpdateMember from '@/components/Template/Member/UpdateMember.vue'

import userStatus from './userStatus'

export default {
  name: 'Member',
  components: {
    UpdateMember
  },

  data() {
    return {
      form: {
        status: '',
        keyword: '',
        tel: ''
      },

      listLoading: false,

      list: [],

      updateMember: {
        visible: false,
        status: 'add',
        data: {}
      },

      userStatus
    }
  },

  created() {
    this.getList()
  },

  methods: {
    getList() {
      this.listLoading = true
      memberList(this.form).then(res => {
        this.list = res.data.data
        this.listLoading = false
      })
    },

    addMember() {
      this.updateMember.status = 'add'
      this.updateMember.visible = true
    },

    editMember(row) {
      this.updateMember.status = 'edit'
      this.updateMember.data = row
      this.updateMember.visible = true
    },

    memberStatusChanged(row, status) {
      memberStatus({
        uid: row.uid,
        username: row.user_name,
        status
      }).then(() => {
        this.$message.success('修改成功')
      })
    }
  }
}
</script>
